package hk.linkcredit.awardservice.award.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hk.linkcredit.awardservice.award.mapper.AwardRecordMapper;
import hk.linkcredit.awardservice.award.domain.entity.AwardRecord;
import hk.linkcredit.awardservice.award.service.IAwardRecordService;
import hk.linkcredit.awardservice.utils.DesensitizeUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;


/**
 * 奖励订单Service业务层处理
 *
 * @author african-loan
 * @date 2022-06-22
 */
@Service
public class AwardRecordServiceImpl extends ServiceImpl<AwardRecordMapper, AwardRecord> implements IAwardRecordService {

    /**
     * 查询奖励订单
     *
     * @param id 奖励订单ID
     * @return 奖励订单
     */
    @Override
    public AwardRecord selectAwardRecordById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询奖励订单列表
     *
     * @param awardRecord 奖励订单
     * @return 奖励订单
     */
    @Override
    public List<AwardRecord> selectAwardRecordList(AwardRecord awardRecord) {
        return this.list(new QueryWrapper<>(awardRecord).last("order by id desc"));
    }

    /**
     * 新增奖励订单
     *
     * @param awardRecord 奖励订单
     * @return 结果
     */
    @Override
    public int insertAwardRecord(AwardRecord awardRecord) {
        awardRecord.setCreateTime(new Date());
        return baseMapper.insert(awardRecord);
    }

    /**
     * 修改奖励订单
     *
     * @param awardRecord 奖励订单
     * @return 结果
     */
    @Override
    public int updateAwardRecord(AwardRecord awardRecord) {
        awardRecord.setUpdateTime(new Date());
        return baseMapper.updateById(awardRecord);
    }

    /**
     * 批量删除奖励订单
     *
     * @param ids 需要删除的奖励订单ID
     * @return 结果
     */
    @Override
    public int deleteAwardRecordByIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除奖励订单信息
     *
     * @param id 奖励订单ID
     * @return 结果
     */
    @Override
    public int deleteAwardRecordById(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public BigDecimal getAmountSum(String userId) {
        return Optional.ofNullable(baseMapper.getAmountSum(userId)).orElse(BigDecimal.ZERO);
    }

    @Override
    public JSONObject getUserRecordInfo(String userId) {
        JSONObject jsonObject = new JSONObject();
        BigDecimal sum = Optional.ofNullable(baseMapper.getAmountSum(userId)).orElse(BigDecimal.ZERO);
        jsonObject.put("amountSum", sum);
        QueryWrapper<AwardRecord> q = new QueryWrapper<AwardRecord>().eq("user_id", userId).orderByDesc("create_time");
        q.select("award_amount", "invited_phone", "create_time");
        List<AwardRecord> awardRecords = list(q);
        awardRecords.stream().forEach(p -> p.setInvitedPhone(DesensitizeUtils.around(p.getInvitedPhone(), 3, 4)));
        jsonObject.put("awardRecords", awardRecords);
        return jsonObject;
    }

    @Override
    public Map<String, JSONObject> getUserInviteRecord(String userId) {
        //根据phone进行分组累计金额
        List<AwardRecord> userInviteRecord = baseMapper.getUserInviteRecord(userId);

        Map<String, JSONObject> map = new HashMap<>();
        userInviteRecord.forEach(e -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("invitedPhone", e.getInvitedPhone());
            jsonObject.put("awardAmount", e.getAwardAmount());
            jsonObject.put("invitedUserId", e.getInvitedUserId());
            map.put(e.getInvitedUserId(), jsonObject);
        });
        return map;
    }

    @Override
    public long rewardedTotal(String bvnAccount, Long actId, Long awardConfigId) {
        QueryWrapper<AwardRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("act_id", actId);
        queryWrapper.eq("invited_bvn", bvnAccount);
        queryWrapper.eq("award_config_id", awardConfigId);
        return Optional.ofNullable(count(queryWrapper.select("DISTINCT batch_no"))).orElse(0L);
    }

    @Override
    public Object awardDistribution(List<AwardRecord> recordList) {


        return null;
    }

}
